package org.microdream.easy.deploy.action;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.microdream.easy.deploy.po.DOrg;
import org.microdream.easy.deploy.service.DOrgService;
import org.microdream.easy.deploy.service.DRentService;
import org.microdream.easy.deploy.service.DSequenceService;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONArray;

import ice.server.comm.BaseAction;
import ice.server.comm.GloubCode;
import ice.server.comm.ParamBulider;
import ice.tool.ResultMap;
import ice.tool.bean.BeanUtil;

@RestController
@RequestMapping("/DOrg")
public class DOrgAction extends BaseAction {

    @Resource
    private DRentService dRentService;
    @Resource
    private DOrgService dOrgService;
    @Resource
    private DSequenceService dSequenceService;

    @GetMapping("getOrgTree")
    public JSONArray getOrgTree(){
        JSONArray jsonArray = new JSONArray();
        jsonArray = dOrgService.getOrgKeyValue("TESTRENT","myId","text");
        return jsonArray;
    }

    @GetMapping("/searchByCode/{key}")
    public DOrg searchByCode(@PathVariable("key")String key){
        return dOrgService.searchByCode(key);
    }

    @PostMapping("")
    public Map add(DOrg dOrg){
        dOrg.setOrgCode(dSequenceService.getNextSequence(GloubCode.ORGCODE.getName(),GloubCode.SUPPLEMENTCODE.getName()));
        dOrg.setRentCode("TESTRENT");
        dOrg.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        dOrg.setIsDelete(0);
        if(StringUtils.isEmpty(dOrg.getOrgFCode())){
            dOrg.setOrgFCode("0");
        }
        return dOrgService.add(dOrg);
    }

    @PutMapping("")
    public Map update(DOrg dOrg){
        DOrg dOrgOld = dOrgService.findSingle(ParamBulider.build().addEqual("orgCode",dOrg.getOrgCode()).get());
        try {
            BeanUtil.copyNotEmptyProprties(dOrg,dOrgOld);
        } catch (Exception e) {
            return ResultMap.build().warningState().message(e.getMessage()).get();
        }
        return dOrgService.upd(dOrgOld);
    }

    @DeleteMapping("/{key}")
    public Map delete(@PathVariable("key")String key){
        if(StringUtils.isEmpty(key)){
            return ResultMap.build().warningState().message("无效数据").get();
        }
        List<String> ids = Arrays.asList(key.split(","));
        return dOrgService.del(ids);
    }
    
    /**
     * select组件选择租户的接口
     * @return
     */
    @GetMapping("/forSelect/{rentCode}")
    public List<DOrg> getOrgForSelect(@PathVariable("rentCode") String rentCode) {
    	return dOrgService.find(ParamBulider.build().addEqual("rentCode", rentCode).get());
	}

    @GetMapping("/getOrgList/{rentCode}")
    public JSONArray getOrgList(@PathVariable("rentCode")String rentCode){
        if (StringUtils.isNotEmpty(rentCode)) {
            return dOrgService.searchOrgKeyValue(rentCode,"","");
        }
        return new JSONArray();
    }
}
